<template>
	<view class="wrap">
		<view class="search">
			<u-search v-model="keywords" @custom="search" @search="search"></u-search>
		</view>
		<scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
			<u-cell-group class="list" :border="false">
				<u-swipe-action :options="options" v-for="(item, index) in list" :key="item.ProblemApproveId" :index="index" @click="optionsClick">
					<u-cell :arrow="true" @click="gotoApprovePage(item)">
						<text slot="title">{{ item.ProblemApproveTitle || item.ProblemId }}</text>
						<!-- <text style="font-size: 12px" slot="label">在手时长：{{ $u.timeFormat(item.ProblemCreDate, 'yyyy-mm-dd') }}</text> -->
					</u-cell>
				</u-swipe-action>
			</u-cell-group>
			<view class="loadmore" @click="loadMore">
				<u-loadmore :status="loadStatus"></u-loadmore>
			</view>
		</scroll-view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			msgType: '',
			keywords: '',
			query: {
				pageIndex: 1,
				pageSize: 10
			},
			list: [],
			count: 0,
			loadStatus: 'loadmore',
			options: [
				{ text: '上传', style: { background: '#414ee0' } },
				{ text: '删除', style: { background: '#dd524d' } }
			]
		};
	},
	onLoad(params) {
		// 获取上一个页面传过来的参数
		let msgType = params.MsgType;
		this.msgType = msgType;
	},
	onShow() {
		this.list = [];
		this.loadList();
		if (uni.getStorageSync('refreshList') === true) {
			uni.removeStorageSync('refreshList');
			this.search('');
		}
	},
	onBackPress(options) {
	},
	methods: {
		fabClick() {
			uni.navigateTo({
				url: '../card/card?status=' + this.$GC.STATUS.add
			});
		},

		loadMore() {
			this.loadStatus = 'loading';
			setTimeout(() => {
				this.query.pageIndex += 1;
				this.loadList();
			}, 100);
		},
		loadList() {
			let queryParam = this.query;
			if(this.msgType == 'check') {
				// 问题验收
				queryParam.MsgType = '问题验收'
			}
			queryParam.ProblemApproveIsFinsh = false;
			this.$api
				.queryProblemApprList(queryParam)
				.then((res) => {
					if (!res.data || res.data.length == 0) {
						this.loadStatus = 'nomore';
						return;
					}

					if (res.success) {
						this.list = this.list.concat(res.data);
						let pageInfo = res.pageInfo;

						this.count = pageInfo.totalSize;
						this.query.pageIndex = pageInfo.pageIndex;
						this.query.pageSize = pageInfo.pageSize;

						if (this.query.pageIndex == pageInfo.totalPage) {
							this.loadStatus = 'nomore';
						} else {
							this.loadStatus = 'loadmore';
						}
					}
				})
				.catch((error) => {});
		},
		optionsClick(rowIndex, btnIndex) {
			let self = this,
				row = self.list[rowIndex];
			if (btnIndex == 0) {
				this.navTo('formUpload?id=' + row.id);
			} else if (btnIndex == 1) {
				uni.showModal({
					title: '提示',
					content: '确认要删除该数据吗？',
					showCancel: true,
					success: function (res2) {
						if (res2.confirm) {
							self.$u.api.testData.delete({ id: row.id }).then((res) => {
								self.$u.toast(res.message);
								if (res.result == 'true') {
									self.list.splice(rowIndex, 1);
								}
							});
						}
					}
				});
			}
		},
		search(value) {
			this.list = [];
			this.query.pageIndex = 1;
			this.query.keywords = value;
			this.loadList();
		},
		async gotoApprovePage(item) {
			if (item.ProblemApprovePeriod == '问题筛选') {
				//判断是否是职能检查还是普通流程
				let queryParam = {
					ProblemId: item.ProblemId
				};
				let resp = await this.$api.queryProblemById(queryParam);
				let problemData = resp.data[0];
				
				let prefixUrl = 'takephoto'
				if(problemData.ProblemFlow === '1') {
					prefixUrl = 'funcAudit';
				}
				this.navTo(`/pages/hse/${prefixUrl}/approve/filter?status=${this.$GC.STATUS.approve}&ProblemId=${item.ProblemId}`);
			} else if (item.ProblemApprovePeriod == '问题处理') {
				this.navTo(`/pages/hse/takephoto/approve/handling?status=${this.$GC.STATUS.approve}&ProblemId=${item.ProblemId}`);
			} else if (item.ProblemApprovePeriod == '问题验收') {
				this.navTo(`/pages/hse/takephoto/approve/check?status=${this.$GC.STATUS.approve}&ProblemId=${item.ProblemId}`);
			}
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>
<style lang="scss">
.wrap {
	font-family: 'PingFang SC Regular';
}
page {
	background-color: #f8f8f8;
}
</style>
